package org.jash.sportnews2109.vm

import androidx.lifecycle.MutableLiveData
import dagger.hilt.android.lifecycle.HiltViewModel
import org.jash.common.mvvm.BaseViewModel
import org.jash.common.retrofit
import org.jash.sportnews2109.dao.NewsDao
import org.jash.sportnews2109.entity.News
import org.jash.sportnews2109.net.APIService
import javax.inject.Inject
@HiltViewModel
class BookmarkViewModel @Inject constructor(val newsDao: NewsDao):BaseViewModel() {
    val service by lazy { retrofit.create(APIService::class.java) }
    val newsLiveData by lazy { MutableLiveData<List<News>>() }

    fun loadMyCollect() {
        progress(service.getMyCollect()) {
            if (it.code == 0) {
                newsLiveData.postValue(it.data)
                newsDao.save(*it.data.toTypedArray())
            } else {
                errorLiveData.postValue(RuntimeException(it.msg))
            }
        }
    }

}